package sdk.webview.fmc.com.fmcsdk.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class StringUtils {
    /**
     * 在Activity获取 manifestPlaceholders 的值
     * <activity android:name=".PayTestActivity">
     * <meta-data android:name="test_key" android:value="${AppKey}"/>
     * </activity>
     * <p>
     * 注意:在AndroidManifest.xml 该Activity标签下加上  <meta-data  android:name="label111" android:value="${label}" /><!--这一句起到至关重要作用-->
     *
     * @param context
     * @param key
     * @return
     */
    public static String getActivityPlaceholders(Context context, String key) {

        String placeholdersValues = "";
        try {
            ActivityInfo appInfo = context.getPackageManager().getActivityInfo(((Activity) context).getComponentName(), PackageManager.GET_META_DATA);//Activity中
            if (appInfo == null) {
                return "";
            }
            Bundle metaData = appInfo.metaData;
            if (metaData == null) {
                return "";
            }
            placeholdersValues = metaData.getString(key);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return placeholdersValues;
    }


    /**
     * 在Application获取 manifestPlaceholders 的值
     * <application
     * android:allowBackup="true"
     * android:icon="@mipmap/ic_launcher"
     * android:label="@string/app_name">
     * <meta-data android:name="test_key" android:value="${AppKey}"/>
     * </application>
     *
     * @param context
     * @param key
     * @return
     */
    public static String getApplicationPlaceholders(Context context, String key) {

        String placeholdersValues = "";
        try {
            //services receiver只是改个名字类似 ServiceInfo
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);//Application中
            if (appInfo == null) {
                return "";
            }
            Bundle metaData = appInfo.metaData;
            if (metaData == null) {
                return "";
            }
            placeholdersValues = metaData.getString(key);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return placeholdersValues;
    }


    /**
     * 判断上一次的推送到现在的推送间隔 是否超过5秒
     *
     * @param sharedPreferences
     * @return
     */
    public static boolean hasCha5miao(SharedPreferences sharedPreferences) {
        long lastTime = sharedPreferences.getLong("lastTime", 0);
        long curTime = System.currentTimeMillis();
        boolean has5 = curTime - lastTime >= 5000;
        return has5;
    }

    /**
     * 给手机号码加*
     *
     * @param phoneNumber
     * @return
     */
    public static String maskPhoneNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.length() == 0) {
            return phoneNumber;
        }
        return phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(7);
    }

    /**
     * 转换为 form-data
     *
     * @param requestDataMap
     * @return
     */
    public static Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), requestDataMap.get(key));
            requestBodyMap.put(key, requestBody);
        }
        return requestBodyMap;
    }


    /**
     * 是否需要修改密码
     *
     * @param pwd
     * @return PWD_COMPLAXITY   //密码正则
     */
    public static boolean isPwdPartten(String pwd, String pattern) {
        if (TextUtils.isEmpty(pattern)) {
            return true;
        } else {
            return Pattern.compile(pattern).matcher(pwd).matches();
        }
    }

    public static void showLog(String str) {
        str = str.trim();
        int index = 0;
        int maxLength = 4000;
        String finalString;
        while (index < str.length()) {
            if (str.length() <= index + maxLength) {
                finalString = str.substring(index);
            } else {
                finalString = str.substring(index, maxLength);
            }
            index += maxLength;
            Log.i("shuchang", finalString.trim());
        }
    }

    /**
     * 用于把密码长度变成int【】
     *
     * @param str
     * @return
     */
    public static int[] CommaSeparatedStringToIntArray(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        String[] stringArray = str.split(",");

        if (stringArray == null) {
            return null;
        }

        if (stringArray.length == 1) {
            return null;
        }

        int[] intArray = new int[stringArray.length];

        for (int i = 0; i < stringArray.length; i++) {
            intArray[i] = Integer.parseInt(stringArray[i]);
        }
        // 输出转换后的整数数组
        for (int num : intArray) {
            System.out.print("密码长度-->" + num);
        }

        return intArray;
    }
    /**
     * 获取图片路径最后一个斜杠后面的
     *
     * @param path
     * @return
     */
    public static String lastxiegang(String path) {
//        String path = "/storage/emulated/0/Download/example.png";
        int lastSlashIndex = path.lastIndexOf('/');
        if (lastSlashIndex != -1) {
            String filename = path.substring(lastSlashIndex + 1);
            // 现在filename包含了文件名，即"example.png"
            return filename;
        } else {
            return "";
        }
    }

    public static boolean isEmpty(final CharSequence s) {
        return s == null || s.length() == 0;
    }
}
